//
//  HNHappyFamilyWebController.m
//  XDEducation
//
//  Created by 星道 on 17/6/7.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "HNHappyFamilyWebController.h"
#import "HNHappyFamilyWebModel.h"
#import "HNCreditGoldSelectView.h"
#import <WebKit/WebKit.h>

@interface HNHappyFamilyWebController ()
@property (nonatomic, strong) NSMutableArray *DataArr;
@end

@implementation HNHappyFamilyWebController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    self.title = @"浏览器";

    //选择网地址
    [self RefreshDataWithWebUrlList];

}


#pragma mark - 初始化浏览器
- (void)SetWebViewWithUrlStr:(NSString *)UrlStr
{
    NSString *TempUrlStr = [[UrlStr substringToIndex:7] lowercaseString];
    NSString *TempUrlStr2 = [[UrlStr substringToIndex:8] lowercaseString];

    if (![TempUrlStr isEqualToString:@"http://"] && ![TempUrlStr2 isEqualToString:@"https://"])
    {
        UrlStr = [@"http://" stringByAppendingString:UrlStr];
    }

    UIWebView *WebView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    [WebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:UrlStr]]];
    [self.view addSubview:WebView];
}


#pragma mark - 网络请求
/** 获取网址列表 */
- (void)RefreshDataWithWebUrlList
{
    [MBProgressHUD showHUD];
    NSString *uid = [[AccountTool shareAccountTool].currentAccount.rolecode isEqualToString:@"4"]?[[AccountTool shareAccountTool].currentAccount.invite_uid stringValue]:[AccountTool shareAccountTool].currentAccount.uid;
    NSDictionary *Params = @{
                             @"action":@"getSetting",
                             @"uid"   :uid
                             };
    [XDHttpTool get:BaseURL(@"internet.do?") authCode:[AccountTool shareAccountTool].currentAccount.authCode params:Params success:^(id responseObj) {

        if ([responseObj[@"code"] isEqualToNumber:@1000])
        {
            for (NSDictionary *Dict in responseObj[@"t"])
            {
                [self.DataArr addObject:[HNHappyFamilyWebModel mj_objectWithKeyValues:Dict]];
            }
            [self SelectUrl];
        }
        else
        {
            [MBProgressHUD showError:responseObj[@"msg"]];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD showError:XDNetWorkError];
    }];

}



#pragma mark - 选择窗
- (void)SelectUrl
{

    NSMutableArray *TitleArr = [NSMutableArray array];
    NSMutableArray *UrlArr   = [NSMutableArray array];
    for (HNHappyFamilyWebModel *Model in self.DataArr)
    {
        [TitleArr addObject:Model.addrName];
        [UrlArr addObject:Model.addrUrl];
    }
    HNCreditGoldSelectView *SelectView = [[HNCreditGoldSelectView alloc] initWithTitle_Arr:TitleArr SelectRow:^ (NSString *Title) {
        NSInteger Key = [TitleArr indexOfObject:Title];
        [self SetWebViewWithUrlStr:UrlArr[Key]];
    }];
    [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:SelectView];
}





#pragma mark - Setter
- (NSMutableArray *)DataArr
{
    if (!_DataArr)
    {
        _DataArr = [NSMutableArray array];
    }
    return _DataArr;
}
@end
